package org.example.reemploymentsys.controller;

import org.example.reemploymentsys.entity.Organization;
import org.example.reemploymentsys.entity.PublishList;
import org.example.reemploymentsys.repository.OrganizationRe;
import org.example.reemploymentsys.repository.PublishListRe;
import org.example.reemploymentsys.repository.UserTbRe;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PublishListController {
    @Autowired
    private UserTbRe userTbRe;
    @Autowired
    private PublishListRe publishListRe;
    @Autowired
    private OrganizationRe organizationRe;

    @PostMapping("/getPublishList")
    public List<PublishList> getPublishList(@RequestBody String username){
        System.out.println(username);
        String name= username.replace("\"","").replace("\"","");
        System.out.println(name);
        String OrganizationId= userTbRe.findOrgizationIdByUsername(name);
        System.out.println(OrganizationId);
        return publishListRe.findPublishListByOrganizationId(OrganizationId);

    }
    @PostMapping("/updateStatus/{id}")
    public String togglePublishStatus(
            @PathVariable Long id,
            @RequestBody String status
    ) {
        System.out.println(status);
        String name= status.replace("\"","").replace("\"","");
        System.out.println(name);
        try {
            PublishList publishList = publishListRe.findById(id).orElseThrow();
            publishList.setStatus(name);
            publishListRe.save(publishList);
            return "success";
        } catch (RuntimeException e) {
           return "error";
        }
    }
    @PostMapping("/updatePublish/{id}")
    public String updatePublish(
            @PathVariable Long id,
            @RequestBody PublishList publishList
    ) {
        try {
            PublishList List = publishListRe.findById(id).orElseThrow();
            publishListRe.save(publishList);
            return "success";
        } catch (RuntimeException e) {
            return "error";
        }
    }
    @PostMapping("/deletePublishList/{id}")
    public String deletePublishList(@PathVariable Long id){
        try {
            publishListRe.deleteById(id);
            return "success";
        }catch (RuntimeException e){
            return "error";
        }
    }
    @PostMapping("/addPublishList/{username}")
    public String addPublishList(
            @PathVariable String username,
            @RequestBody PublishList publishList){
        try {
            System.out.println(username);
            String name= username.replace("\"","").replace("\"","");
            System.out.println(name);
            Long OrganizationId= Long.valueOf(userTbRe.findOrgizationIdByUsername(name));
            Organization organization = organizationRe.findById(OrganizationId).orElseThrow();
            publishList.setOrganization(organization);
            publishListRe.save(publishList);
            return "success";
        }catch (RuntimeException e){
            return "error";
        }
    }
}
